<template>
	<view class="">
		<button @click="chooseImage">上传并识别</button>
		<view class="result" v-if="result">
			图片-------------{{base64Image}}
			---------------
			<image :src="base64Image" mode="aspectFill" style="width: 100rpx;height: 100rpx;"></image>
			<text>识别结果：{{ result }}</text>
		</view>
	</view>
</template>

<script>
	import {
		pathToBase64
	} from '../../tools/index'
	export default {
		data() {
			return {
				base64Image: '',
				result: '',
				access_token: ''
			};
		},
		methods: {
			chooseImage() {
				let that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						const tempFilePaths = res.tempFilePaths[0];
						pathToBase64(tempFilePaths)
							.then(base64 => {
								console.log("转换成功==>base64", base64)
								that.base64Image = base64;
								this.uploadImage();
							})
							.catch(error => {
								console.error("转换失败==>", error)
							})
					}
				});
			},
			uploadImage() {
				const params = {
					image: this.base64Image,
					baike_num: '10',
					top_num: '10',
					'access_token': '24.ba393f17a3ce2693345aedc4111c17d1.2592000.1722239336.282335-35389279'
				};

				const header = {
					'Content-Type': 'application/x-www-form-urlencoded',
				};
				let that = this
				uni.request({
					url: 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal',
					method: 'POST',
					data: params,
					header: header,
					success: (res) => {
						if (res.data && res.data.result) {
							this.result = res.data.result;
							console.log(res)
							if(uni.getStorageSync('historyList')){
								let list = uni.getStorageSync('historyList')
								list.push({
									imgurl:that.base64Image,
									result:res.data.result,
									isChecked:false,
									createTime:that.getDate()
								})
								uni.setStorageSync('historyList',list)
								uni.navigateTo({
									url:'/pages/detail/index'
								})
							}else{
								let list =[]
								list.push({
									imgurl:that.base64Image,
									result:res.data.result,
									isChecked:false,
									createTime:that.getDate()
								})
								uni.setStorageSync('historyList',list)
								uni.navigateTo({
									url:'/pages/detail/index'
								})
							}
							
						} else {
							this.result = '无法识别';
						}
					},
					fail: () => {
						this.result = '请求失败';
					}
				});
			},
			getDate(){
				 var now = new Date();
				    var year = now.getFullYear();
				    var month = now.getMonth() + 1; // 月份是从0开始的，所以需要+1
				    var day = now.getDate();
				    var hours = now.getHours();
				    var minutes = now.getMinutes();
				    var seconds = now.getSeconds();
				 
				    // 补零操作，确保月、日、时、分、秒都是两位数
				    month = (month < 10 ? "0" : "") + month;
				    day = (day < 10 ? "0" : "") + day;
				    hours = (hours < 10 ? "0" : "") + hours;
				    minutes = (minutes < 10 ? "0" : "") + minutes;
				    seconds = (seconds < 10 ? "0" : "") + seconds;
				 
				    return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			},
			encodeParams() {
				// 使用urlencode后的数据创建请求体
				const params = {
					image: this.base64Image,
					image_type: 'BASE64'
				};
				return Object.keys(params).map((key) => {
					return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
				}).join('&');
			}
		}
	};
</script>

<style scoped>
	.container {
	}

	button {
		margin-top: 10px;
	}
	.result {
		margin-top: 20px;
	}
</style>